package Project;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LoginUi {
	JFrame jf = new JFrame();
	JPanel[] pan;
	JPanel BicPan;

	JLabel titlelabel, namelabel, numlabel, checklabel, gonglabel;
	JButton membutton, logbutton, canbutton;
	Checkbox student, professor;
	JTextField nametext, numtext;
	CheckboxGroup cgroup;

	public void LoginUI(Point p) {
		jf.setLocation(p);
		jf = new JFrame("학사 관리");
		BicPan = new JPanel();
		BicPan.setLayout(new GridLayout(3, 1));

		pan = new JPanel[7];
		for (int i = 0; i < pan.length; i++) {
			pan[i] = new JPanel();
			// pan[i].setLayout(new BorderLayout());
		}

		titlelabel = new JLabel("안녕하세요!", JLabel.CENTER);
		Font font = new Font("dialog", Font.BOLD, 20);
		titlelabel.setFont(font);
		titlelabel.setForeground(new Color(123, 45, 56));
		namelabel = new JLabel("이름 :   ");
		numlabel = new JLabel("번호 :   ");
		checklabel = new JLabel("선택사항");
		Font font1 = new Font("dialog", Font.BOLD, 15);
		checklabel.setFont(font1);
		membutton = new JButton("회원가입");
		logbutton = new JButton("로그인");
		canbutton = new JButton("취소");

		nametext = new JTextField("");
		numtext = new JTextField("");

		cgroup = new CheckboxGroup();
		student = new Checkbox("학생", cgroup, true);
		professor = new Checkbox("교수", cgroup, false);

		jf.pack();
		jf.setVisible(true);
		jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

		// 회원가입 버튼
		membutton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Point p = jf.getLocation();
				jf.setVisible(false);
				// System.out.println("계속 수행...");
				new Dinamic(p);
			}
		});

		// 로그인 버튼
		logbutton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DAOTest test = new DAOTest();
				String name = nametext.getText();
				String num = numtext.getText();
				Point p = jf.getLocation();
				boolean state;
				
				// 선택된 항목이 학생인지 교수인지 판별
				if (student.getState() == true) {
					state = true;
				} else {
					state = false;
				}
				
				if (test.Login(name, num, state) == true) {
					if(state==false)
					{
						jf.setVisible(false);
						ProLogin pro = new ProLogin(p, num);
						pro.prologbutton();
					}else{
						System.out.println("학생 로그인");
						StuLogin sl = new StuLogin(p, num);
						sl.stulogbutton();
						jf.setVisible(false);
					}
				}
			}
		});
	}

	public void uiframe() {
		pan[0].add(titlelabel);
		pan[1].add(checklabel);
		pan[1].add(student);
		pan[1].add(professor);
		pan[2].setLayout(new GridLayout(5, 1));
		pan[2].add(new JLabel(""));
		pan[2].add(namelabel);
		pan[2].add(new JLabel(""));
		pan[2].add(numlabel);
		pan[2].add(new JLabel(""));
		pan[3].setLayout(new GridLayout(5, 1));
		pan[3].add(new JLabel(""));
		pan[3].add(nametext);
		pan[3].add(new JLabel(""));
		pan[3].add(numtext);
		pan[3].add(new JLabel(""));
		pan[4].setLayout(new BorderLayout());
		pan[4].add(pan[2], "West");
		pan[4].add(pan[3], "Center");

		// pan[5].setLayout(new GridLayout(1, 3));
		membutton.setPreferredSize(new Dimension(100, 30));
		logbutton.setPreferredSize(new Dimension(100, 30));
		canbutton.setPreferredSize(new Dimension(100, 30));
		pan[5].add(membutton);
		pan[5].add(logbutton);
		pan[5].add(canbutton);

		pan[6].setLayout(new BorderLayout());
		pan[6].add(pan[1], "North");
		pan[6].add(pan[4], "Center");

		BicPan.add(pan[0]);
		BicPan.add(pan[6]);

		JPanel subWest = new JPanel();
		JPanel subNorth = new JPanel();
		JPanel subSouth = new JPanel();
		JPanel subEast = new JPanel();

		subWest.setPreferredSize(new Dimension(40, 40));
		subNorth.setPreferredSize(new Dimension(40, 40));
		subSouth.setPreferredSize(new Dimension(40, 40));
		subEast.setPreferredSize(new Dimension(40, 40));

		subWest.add(new JLabel(""));
		subNorth.add(new JLabel(""));
		subSouth.add(new JLabel(""));
		subEast.add(new JLabel(""));

		jf.add(subWest, "West");
		jf.add(subNorth, "North");
		jf.add(subSouth, "South");
		jf.add(subEast, "East");

		jf.add(BicPan, "Center");
		jf.add(subSouth, "South");
		jf.add(pan[0], "North");
		jf.add(pan[6], "Center");
		jf.add(pan[5], "South");

		jf.setResizable(false);// 크기 조절 불가

		jf.setSize(350, 300);
		jf.setLocation(
				((jf.getToolkit().getScreenSize()).width - jf.getWidth()) / 2,
				((jf.getToolkit().getScreenSize()).height - jf.getHeight()) / 2);
		jf.setVisible(true);
	}

	public static void main(String[] args) {
		Point p = new Point();
		p.setLocation(100, 100);
		LoginUi ui = new LoginUi();
		ui.LoginUI(p);
		ui.uiframe();
	}

}
